package com.example.myapplicationndk.retrofit;

import android.util.Log;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Retrofits {

    private static String tag = Retrofits.class.getSimpleName();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://fanyi.youdao.com/") //设置网络请求的Url地址
            .addConverterFactory(GsonConverterFactory.create()) //设置数据解析器
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();


    /***
     *
     */
    public void RxjavaRetrofit() {

        RetrofitUtil.getTestService().getRxData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JsonBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(JsonBean jsonBean) {
                        Log.d("444", jsonBean.toString());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        // 取消
        RetrofitUtil.getTestService().getRxData().unsubscribeOn(Schedulers.io());

    }

    public void testCancel() {


        // 创建 网络请求接口 的实例
        Api httpService = retrofit.create(Api.class);

        Call<JsonBean> call = httpService.getData();

        call.enqueue(new Callback<JsonBean>() {
            @Override
            public void onResponse(Call<JsonBean> call, Response<JsonBean> response) {

            }

            @Override
            public void onFailure(Call<JsonBean> call, Throwable t) {

            }
        });

    }
}
